package com.bt7274.service.impl;

/**
 * @Data 2025/6/10 11:05
 * @Author hp
 * @Version 1.0
 * @Description TorrentCommentServiceImpl
 */

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bt7274.mapper.TorrentCommentMapper;
import com.bt7274.mapper.UserMapper;
import com.bt7274.pojo.TorrentComment;
import com.bt7274.pojo.User;
import com.bt7274.pojo.bo.TorrentCommentBO;
import com.bt7274.pojo.vo.TorrentCommentVO;
import com.bt7274.service.TorrentCommentService;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class TorrentCommentServiceImpl implements TorrentCommentService {
    @Resource
    private TorrentCommentMapper torrentCommentMapper;

    @Resource
    private UserMapper userMapper;

    @Override
    public List<TorrentCommentVO> getCommentsByTorrentId(Long torrentId) {
        List<TorrentComment> torrentComments = torrentCommentMapper.selectList(
                new QueryWrapper<TorrentComment>().eq("torrent_id", torrentId)
        );
        // 创建一个新的列表用于存储转换后的 VO 对象
        List<TorrentCommentVO> voList = new ArrayList<>();
        for (TorrentComment comment : torrentComments) {
            TorrentCommentVO commentVO = new TorrentCommentVO();
            BeanUtils.copyProperties(comment, commentVO);
            commentVO.setUsername(userMapper.selectById(comment.getUserId()).getUsername());
            if(!StringUtils.isBlank(comment.getReplyId())){
                commentVO.setReplyTo(userMapper.selectById(comment.getReplyId()).getUsername());
            }
            voList.add(commentVO);
        }
        // 返回转换后的 VO 列表
        return voList;
    }

    @Override
    public TorrentCommentVO addComment(TorrentCommentBO commentBO) {
        TorrentComment comment = new TorrentComment();
        BeanUtils.copyProperties(commentBO, comment);
        if(!StringUtils.isBlank(commentBO.getReplyName())){
            comment.setReplyId(userMapper.selectOne(
                    new QueryWrapper<User>().eq("username",commentBO.getReplyName())
            ).getId());
        }
        torrentCommentMapper.insert(comment);
        TorrentCommentVO commentVO = new TorrentCommentVO();
        BeanUtils.copyProperties(comment, commentVO);
        commentVO.setUsername(userMapper.selectById(comment.getUserId()).getUsername());
        commentVO.setReplyTo(commentBO.getReplyName());
        commentVO.setCreatedAt(LocalDateTime.now());
        return commentVO;
    }

    @Override
    @Transactional
    public void deleteCommentById(Long commentId) {
        torrentCommentMapper.deleteById(commentId);
    }

    @Override
    public boolean commentExists(Long commentId) {
        return torrentCommentMapper.selectById(commentId) != null;
    }
}
